<?php
namespace user\service;

use common\service\CommonService;
use herosphp\core\Log;
use herosphp\string\StringUtils;
use user\service\interfaces\IUserService;
use herosphp\core\Loader;

Loader::import('user.service.interfaces.IUserService');

/**
 * MPE终端会员服务接口实现
 * @package user\service
 * @author yangjian<yangjian102621@gmail.com>
 */
class UserService extends CommonService implements IUserService {

    private $lastErrorMsg;
    public function add( $data ){
        $this->beginTransaction();
        $lastId = parent::add($data);
        if( !$lastId ){
            $this -> rollback();
            return false;
        }
        $extendsData['user_id'] = $lastId;
        $extendsData['addtime'] = date('Y-m-d H:i:s',time());
        $model = Loader::model('UserExtends');
        if( $model -> add($extendsData)){
            $this->commit();
            return true;
        }else{
            $this -> rollback();
            return false;
        }
    }

    public function delete($id)
    {
        $this -> beginTransaction();
        if(parent::delete($id) !== false){
            $model = Loader::model('UserExtends');
            if( $model->deletes(array('user_id'=>$id) !== false) ){
                $this->commit();
                return true;
            }
        }
        $this -> rollback();
        return false;
    }

    public function addByWx( $wx_id, $from ){
        $model = Loader::model('UserExtends');
        if( $model -> count(array('wx_id'=>$wx_id))){
            $this -> lastErrorMsg = "创建失败，微信号 {$wx_id} 已经存在";
            Log::error($this->lastErrorMsg);
            return true;
        }
        $data['id'] = StringUtils::genGlobalUid();
        $data['from'] = $from;
        $data['status'] = 1;
        $data['addtime'] = date('Y-m-d H:i:s',time());
        $data['edittime'] = date('Y-m-d H:i:s',time());

        $this->beginTransaction();
        $lastId = parent::add($data);
        if( !$lastId ){
            $this -> rollback();
            return false;
        }

        $extendsData['user_id'] = $lastId;
        $extendsData['wx_id'] = $wx_id;
        $extendsData['addtime'] = date('Y-m-d H:i:s',time());

        if( $model -> add($extendsData)){
            $this->commit();
            return true;
        }else{
            $this -> rollback();
            return false;
        }
    }

    /**
     * 用户认证，操作失败返回false，操作成功返回user_id
     * @param $username
     * @param $password
     * @param $wx_id
     * @return bool
     */
    public function authUser($username,$password,$wx_id){
        //如果已经存在，就直接返回user_id
        if( $this -> count(array('username'=>$username)) ){
            $this -> lastErrorMsg = "认证失败，手机号号 {$username} 已经存在";
            Log::error($this->lastErrorMsg);
            return false;
            //$item = $this -> getItem(array('username'=>$username),"id","");
            //return $item['id'];
        }
        $model = Loader::model('UserExtends');
        $item = $model -> getItem(array('wx_id'=>$wx_id),"user_id","");
        $user_id = $item['user_id'];
        if( !$user_id ){
            $this -> lastErrorMsg = '认证失败，手机号：'.$username.',未发现该用户微信id';
            Log::error($this->lastErrorMsg);
            return false;
        }
        $data['username'] = $username;
        $data['name'] = 'saas_'.substr($data['username'],4);;
        $data['password'] = $password;
        $data['edittime'] = date('Y-m-d H:i:s');
        $data['name'] = 'saas_'.substr($data['username'],4);
        if( $this -> update($data,$user_id)=== false){
            $this -> lastErrorMsg = '认证失败，手机号：'.$username.',写入数据库失败';
            Log::error($this->lastErrorMsg);
            return false;
        }
        return $user_id;
    }

    public function getItem($condition, $fields, $order)
    {
        $item = parent::getItem($condition, $fields, $order);
        if( !$item )return false;
        $model = Loader::model('UserExtends');
        $extends = $model -> getItem(array('user_id'=>$item['id']),"*","");
        if( !$extends )return false;
        $item += $extends;
        return $item;
    }

    /**
     * @param $wx_id
     * @return bool
     */
    public function getItemByWeiXin($wx_id ){
        $model = Loader::model('UserExtends');
        $extends = $model -> getItem(array('wx_id'=> $wx_id),"*","");
        if( !$extends )return false;
        $item = parent::getItem(array('id'=>$extends['user_id']),"*","");
        if( !$item )return false;
        $item += $extends;
        return $item;
    }

    public function getErrorMsg(){
        return $this->lastErrorMsg;
    }
}
